/* Copyright (C) 2009 Vlad Roubtsov. All rights reserved. */

#include "vr/exception/exceptions.hpp"
#include "vr/util/logger.hpp"
#include "vr/util/utility.hpp"

#include "vr/deject/factory/component_factory.hpp"
using std::string;

using namespace vr::deject::factory;
using namespace vr::exception;
using namespace vr::utility;

//----------------------------------------------------------------------------

component_factory::creator_map &
component_factory::singleton ()
{
    static creator_map CREATE;

    return CREATE;
}

bool
component_factory::register_adaptor (const string & implID, creator_t * creator)
{
    LOG_INFO ("registering [" << implID << " -> " << & creator << "]");

    singleton ().insert (creator_map::value_type (implID, creator));

    return true;
}

component_base *
component_factory::create (const string & implID, const deject::component_id_type & ID, const ptr_vector<dependency> & deps)
{
    const creator_map & create = singleton ();

    creator_map::const_iterator i = create.find (implID);

    if (LIKELY (i != create.end ()))
        return i->second (ID, deps);

    __VR_THROW_X (std::domain_error, "cannot instantiate [" + implID + "]: not registered with the factory");
}
//----------------------------------------------------------------------------
